package com.auxgroup.smarthome.app.service;

import com.auxgroup.bridge.app.inner.vo.FamilyCenterDeviceVo;
import com.auxgroup.bridge.app.inner.vo.ShareResultVo;
import com.auxgroup.bridge.app.inner.vo.ShareUserListVo;
import com.auxgroup.bridge.app.inner.vo.SubShareUserVo;
import com.auxgroup.smarthome.app.filter.PermissionAppService;
import com.auxgroup.smarthome.app.service.inner.DeviceShareInnerService;
import com.auxgroup.smarthome.openapi.responsebody.UserTokenMsg;
import com.auxgroup.smarthome.redis.config.ObjectRedis;
import com.auxgroup.smarthome.web.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/7/29 9:22.
 */
@Service
public final class DeviceShareService {
    @Autowired
    private DeviceShareInnerService deviceShareInnerService;
    @Autowired
    private PermissionAppService permissionAppService;
    @Autowired
    private ObjectRedis objectRedis;

    public ApiResponse<List<ShareUserListVo>> getShareUserList(String deviceId) {
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.getSharing(userTokenMsg.getUid(), deviceId);
    }

    public ApiResponse<List<SubShareUserVo>> getSubShareUserList() {
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.getSubShareUserList(userTokenMsg.getUid());
    }

    public ApiResponse<ShareResultVo> createSharing(String deviceIds, Integer userTag) {
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);

        return deviceShareInnerService.createSharing(userTokenMsg.getUid(), deviceIds, userTag);
    }

    public ApiResponse relieveSharing(String shareId) {
        String appId = permissionAppService.getAppId();
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.relieveSharing(appId, userTokenMsg.getToken(), shareId);
    }

    public ApiResponse scanSharing(String qrContent) {
        String appId = permissionAppService.getAppId();
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.scanSharing(appId, userTokenMsg.getToken(), userTokenMsg.getUid(), qrContent);
    }

    public ApiResponse getFamilySharing() {
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.getFamilySharing(userTokenMsg.getUid());
    }

    public ApiResponse getFriendSharing() {
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.getFriendSharing(userTokenMsg.getUid());
    }

    public ApiResponse<List<FamilyCenterDeviceVo>> getUserShareDeviceList(String uid, Integer userTag, String batchNo) {
        String clientId = permissionAppService.getClientId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.getUserShareDeviceList(userTokenMsg.getUid(), uid, userTag, batchNo);
    }

    public ApiResponse relieveUserSharingByOwner(String uid, Integer userTag, String batchNo, String deviceId) {
        String clientId = permissionAppService.getClientId();
        String appId = permissionAppService.getAppId();
        UserTokenMsg userTokenMsg = objectRedis.get(clientId, UserTokenMsg.class);
        return deviceShareInnerService.relieveUserSharingByOwner(appId, userTokenMsg.getToken(), userTokenMsg.getUid(), uid, userTag, batchNo, deviceId);
    }

    /**
     * 第三方登录分享
     * @param uid
     * @param deviceIds
     * @param userTag
     * @return
     */
    public ApiResponse<ShareResultVo> createSharingOtherLogin(String uid,String deviceIds, Integer userTag) {
        return deviceShareInnerService.createSharing(uid, deviceIds, userTag);
    }

}
